﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PMCT
{
    public partial class ItemForm : Form
    {
        public ConfigRoot curRoot;
        public Item curItem;
        public ItemForm()
        {
            InitializeComponent();
        }

        private void ItemForm_Load(object sender, EventArgs e)
        {
            textBox1.Text = curItem.name;

            // input
            checkedListBox1.Items.Clear();
            Guid[] arr = new Guid[curItem.input.Count];
            curItem.input.CopyTo(arr);
            foreach (Unit u in curRoot.listUnit)
            {
                if (u.isTool == false)
                {
                    string fn = System.IO.Path.GetFileName(u.uri);
                    string str = u.name + " - " + fn;
                    int idx = checkedListBox1.Items.Add(str, CheckState.Unchecked);
                    foreach (Guid g in arr)
                    {
                        if (u.guid == g)
                            checkedListBox1.SetItemChecked(idx, true);
                    }
                }
            }

            // output
            checkedListBox2.Items.Clear();
            foreach (Unit u in curRoot.listUnit)
            {
                if (u.isTool == false)
                {
                    string fn = System.IO.Path.GetFileName(u.uri);
                    string str = u.name + " - " + fn;
                    int idx = checkedListBox2.Items.Add(str, CheckState.Unchecked);
                    foreach (Guid g in curItem.output)
                    {
                        if (u.guid == g)
                            checkedListBox2.SetItemChecked(idx, true);
                    }
                }
            }

            //// tools
            //checkedListBox3.Items.Clear();
            //foreach (Unit u in curRoot.listUnit)
            //{
            //    if (u.isTool == true)
            //    {
            //        string fn = System.IO.Path.GetFileName(u.uri);
            //        string str = u.name + " - " + fn;
            //        int idx = checkedListBox3.Items.Add(str, CheckState.Unchecked);
            //        foreach (Guid g in curItem.tools)
            //        {
            //            if (u.guid == g)
            //                checkedListBox3.SetItemChecked(idx, true);
            //        }
            //    }
            //}

            // dimension
            comboBox1.Items.Clear();
            foreach (Dimension d in curRoot.listDimension)
            {
                comboBox1.Items.Add(d.name);
            }
            if (comboBox1.Items.Count > 1)
                comboBox1.SelectedIndex = 0;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            checkedListBox4.Items.Clear();
            Dimension d = curRoot.listDimension[comboBox1.SelectedIndex];
            Guid[] arr = new Guid[curItem.dimension.Count];
            curItem.dimension.CopyTo(arr);
            foreach (DimensionItem di in d.list)
            {
                int idx = checkedListBox4.Items.Add(di.name);
                foreach (Guid g in arr)
                {
                    if (di.guid == g)
                        checkedListBox4.SetItemChecked(idx, true);
                }
            }
        }

        private void checkedListBox4_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            Dimension d = curRoot.listDimension[comboBox1.SelectedIndex];
            if (d.isMultiple)
            {
                // save for multiple
                DimensionItem di = d.list[e.Index];
                if (e.NewValue == CheckState.Checked) // add guid
                {
                    if (!curItem.dimension.Contains(di.guid))
                        curItem.dimension.Add(di.guid);
                }
                else if (e.NewValue == CheckState.Unchecked) // remove guid
                    curItem.dimension.Remove(di.guid);
                return;
            }

            int oldIdx = -1;
            if (e.NewValue == CheckState.Checked)
            {
                for (int i = 0; i < checkedListBox4.Items.Count; i++)
                {
                    if (i != e.Index)
                    {
                        if (checkedListBox4.GetItemChecked(i) == true)
                        {
                            oldIdx = i;
                            checkedListBox4.SetItemChecked(i, false);
                        }
                    }
                }
            }

            if (oldIdx != -1) // remove old
            {
                DimensionItem di = d.list[oldIdx];
                curItem.dimension.Remove(di.guid);
            }

            {
                DimensionItem di = d.list[e.Index];
                if (e.NewValue == CheckState.Checked) // add guid
                {
                    if (!curItem.dimension.Contains(di.guid))
                        curItem.dimension.Add(di.guid);
                }
                else if (e.NewValue == CheckState.Unchecked) // remove guid
                    curItem.dimension.Remove(di.guid);
            }
        }

        private void checkedListBox123_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            CheckedListBox clb = (CheckedListBox)sender;
            List<Guid> list;

            if (clb == checkedListBox1)
                list = curItem.input;
            else if (clb == checkedListBox2)
                list = curItem.output;
            //else if (clb == checkedListBox3)
            //    list = curItem.tools;
            else
                return;

            Guid g = curRoot.listUnit[e.Index].guid;
            if (e.NewValue == CheckState.Checked) // add guid
            {
                if (!list.Contains(g))
                    list.Add(g);
            }
            else if (e.NewValue == CheckState.Unchecked) // remove guid
                list.Remove(g);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == null || textBox1.Text == "")
                curItem.name = "空";
            else
                curItem.name = textBox1.Text;
        }
    }
}
